package com.kxq.zhuoyue.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kxq.zhuoyue.pageUtilEntity.Page;
import com.kxq.zhuoyue.po.Forms;
import com.kxq.zhuoyue.service.FormsService;
import com.kxq.zhuoyue.util.result.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xjh
 * @since 2023-09-01
 */
@Slf4j
@RestController
@RequestMapping("/forms")
public class FormsController {

    @Autowired
    private FormsService formsService;
    /**
     * 查询
     * @return
     */
    @PostMapping("/getByTitle")
    public AjaxResult getByTitle(@RequestBody Page page){
//        log.info(page.toString());
        Map map = new HashMap<>();
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        LambdaQueryWrapper<Forms> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Forms::getFormsTitle,page.getFormsTitle());
        List<Forms> list = formsService.list(queryWrapper);
//        List list = new ArrayList<>();
        PageInfo pageInfo = new PageInfo<>(list);
        map.put("data",list);
        map.put("pageSize",pageInfo.getPageSize());
        map.put("total",pageInfo.getTotal());
        return AjaxResult.success("查询成功",map);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody Forms forms){
        forms.setFormsStatus("待审核");
        boolean save = formsService.save(forms);
        if (save){
            return AjaxResult.success("添加表单成功 ！");
        } else {
            return AjaxResult.success("添加表单失败 ！");
        }
    }

    @PostMapping("/update")
    public AjaxResult update(@RequestBody Forms forms){
        boolean update = formsService.updateById(forms);
        if (update){
            return AjaxResult.success("更新表单成功 ！");
        } else {
            return AjaxResult.success("更新表单失败 ！");
        }
    }

    @PostMapping("/delete/{formsId}")
    public AjaxResult delete(@PathVariable Integer formsId){
        boolean b = formsService.removeById(formsId);
        if (b){
            return AjaxResult.success("删除成功 ！");
        } else {
            return AjaxResult.success("删除失败 ！");
        }
    }
}

